package com.bclz.cat.nouniquekey;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.log4j.Logger;

/**
 *  归并
 * 输入[id,name,count]----------非唯一键
 */
public class CatApplicationStage1 extends Configured implements Tool {

    private static Logger log = Logger.getLogger(CatApplicationStage1.class);

    @Override
    public int run(String[] args) throws Exception {

        Configuration conf=getConf();

        Job job=Job.getInstance(conf,"CAT AGGREGATE ");

        job.setJarByClass(getClass());
        job.setMapperClass(CatMapper1.class);
        job.setReducerClass(CatReducer1.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean b = job.waitForCompletion(true);

        return b?0:1;
    }

    public static void main(String[] args) throws Exception {


        if(args.length!=2){
            throw new IllegalArgumentException("Usage: CatApplicationStage1 [inputdir] [outputdir] ");
        }
        log.info(String.format("\n inputdir: %s \n outputdir: %s ",args[0],args[1]));
        int code = ToolRunner.run(new CatApplicationStage1(), args);
        System.exit(code);
    }


}
